package com.avengers.core;

import com.avengers.core.kit.ControllerKit;
import com.avengers.job.JobNoKit;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

/**
 * @author yu.wang
 * @since 2018-04-03 下午6:20
 */
public class AvengersBaseController extends Controller {
    private static final Logger logger = LoggerFactory.getLogger(AvengersBaseController.class);

    protected void renderRet(Ret ret) {
        renderJson(ret.set("jobNo", JobNoKit.get()));
    }

    @Override
    public String[] getParaValues(String name) {
        return ControllerKit.getParaValues(getRequest(), name);
    }

    /**
     * 获取客户端IP
     */
    public String getClientIp() {
        return ControllerKit.getClientIp(getRequest());
    }

    /**
     * 获取来源ActionKey
     */
    public String getRefererActionKey() {
        return ControllerKit.getRefererActionKey(getRequest());
    }

    /**
     * 是否异步请求
     */
    public boolean isAjax() {
        return ControllerKit.isAjax(getRequest());
    }

    /**
     * 是否为移动端请求
     */
    public boolean isMobile() {
        return ControllerKit.isMobile(getRequest());
    }

    /**
     * 是否为微信端请求
     */
    public boolean isWechat() {
        return ControllerKit.isWeChat(getRequest());
    }

    /**
     * 图片地址
     */
    protected void renderImage(URL url) {
        try {
            renderImage(url.openStream());
            renderNull();
        } catch (Exception e) {
            logger.error("url_{} get image route error: {}", url.toString(), e.getMessage(), e);
            renderError(404);
        }
    }

    protected void renderImage(File imageFile) {
        try {
            FileInputStream fileInputStream = new FileInputStream(imageFile);
            renderImage(fileInputStream);
            renderNull();
        } catch (Exception e) {
            logger.error("file_{} get image route error: {}", imageFile.getAbsolutePath(), e.getMessage(), e);
            renderError(404);
        }
    }

    protected void renderImage(String filePath) {
        try {
            FileInputStream fileInputStream = new FileInputStream(filePath);
            renderImage(fileInputStream);
            renderNull();
        } catch (Exception e) {
            logger.error("filePath_{} get image route error: {}", filePath, e.getMessage(), e);
            renderError(404);
        }
    }

    private void renderImage(InputStream inputStream) throws Exception {
        if (null == inputStream) {
            renderError(404);
            return;
        }
        OutputStream out = null;
        try {
            HttpServletResponse resp = getResponse();
            resp.setContentType("image/jpeg");

            out = resp.getOutputStream();
            int len = 0;
            byte[] b = new byte[1024];
            while ((len = inputStream.read(b)) != -1) {
                out.write(b, 0, len);
            }
            out.flush();
        } finally {
            if (null != out) {
                out.close();
            }
            inputStream.close();
        }
        renderNull();
    }
}
